const express = require("express");
const router = express.Router();
const newsDetail = require("../../model/newsDetail");
const JwtUtil = require("../../utils/jwt");

router.get("/list", async (req, res) => {
    await newsDetail
        .list()
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
router.get("/newsDetail", async (req, res) => {
    await newsDetail
        .newsDetail()
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
router.get("/newsClick", async (req, res) => {
    let id = req.query.id;
    console.log(req.query, 555);
    await newsDetail
        .newsClick(id)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
router.get("/selectComment", async (req, res) => {
    // comment_id 新闻id
    let comment_id = req.query.comment_id;
    let db_id = req.query.db_id;
    await newsDetail
        .selectComment(comment_id, db_id)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
router.get("/searchUserNickName", async (req, res) => {
    // comment_id 新闻id
    let user_id = req.query.user_id;
    await newsDetail
        .searchUserNickName(user_id)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
router.post("/addComment", async (req, res) => {
    let token = req.headers["token"];
    let jwt = new JwtUtil(token);
    let result = jwt.verifyToken();
    console.log(result, "token");
    if (result == "err") {
        res.send({
            code: -1,
            msg: "登录已过期,请重新登录。",
        });
    } else {
        let { comment_id, content, create_time, user_id, db_id } = req.body;
        console.log("req.body", comment_id, content, create_time, user_id, db_id);
        await newsDetail
            .addComment(comment_id, content, create_time, user_id, db_id)
            .then((data) => {
                res.send(data);
            })
            .catch((error) => {
                res.send(error);
            });
    }
});
router.post("/newsSearch", async (req, res) => {
    // comment_id 新闻id
    // console.log(req.query);
    let { content } = req.body;
    await newsDetail
        .newsSearch(content)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
// SELECT * from students where name like '%小%';
router.get("/newsImgList", async (req, res) => {
    await newsDetail
        .newsImgList()
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
router.get("/newsImgCom", async (req, res) => {
    let { id } = req.query;
    console.log(id);
    await newsDetail
        .newsImgCom(id)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
router.post("/hotComment", async (req, res) => {
    let { id } = req.body;
    await newsDetail
        .hotComment(id)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
router.post("/addLike", async (req, res) => {
    let { user_id, user_db, comment_id, detail_id } = req.body;
    console.log(user_id, user_db, comment_id, detail_id);
    await newsDetail
        .addLike(user_id, user_db, comment_id, detail_id)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
router.post("/changeLike", async (req, res) => {
    let { user_db, detail_id } = req.body;
    console.log(user_db, detail_id);
    await newsDetail
        .changeLike(user_db, detail_id)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
router.post("/selectLike", async (req, res) => {
    let { user_db, detail_id, user_id } = req.body;
    console.log(user_db, detail_id, user_id);
    await newsDetail
        .selectLike(user_db, detail_id, user_id)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
// newNewsList
router.get("/newNewsList", async (req, res) => {
    await newsDetail
        .newNewsList()
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
router.get("/userCom", async (req, res) => {
    let { user_id } = req.query;
    await newsDetail
        .userCom(user_id)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
router.get("/userLike", async (req, res) => {
    let { user_id } = req.query;
    await newsDetail
        .userLike(user_id)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
router.get("/userWeb", async (req, res) => {
    let { user_id } = req.query;
    await newsDetail
        .userWeb(user_id)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
router.post("/undateUserWeb", async (req, res) => {
    let { user_id, comment_id, comment_db, comment } = req.body;
    console.log(user_id, comment_id, comment_db, comment);
    await newsDetail
        .undateUserWeb(user_id, comment_id, comment_db, comment)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
// searchNewsLike
router.get("/searchNewsLike", async (req, res) => {
    // comment_id 新闻id
    let { user_db, detail_id } = req.query;
    await newsDetail
        .searchNewsLike(user_db, detail_id)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
router.post("/insertCollect", async (req, res) => {
    let { user_id, comment_id, comment_db } = req.body;
    console.log(user_id, comment_id, comment_db);
    await newsDetail
        .insertCollect(user_id, comment_id, comment_db)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
// selectCollect
router.post("/selectCollect", async (req, res) => {
    let { user_id, comment_id, comment_db } = req.body;
    console.log(user_id, comment_id, comment_db);
    await newsDetail
        .selectCollect(user_id, comment_id, comment_db)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
// selectCollectCount
router.post("/selectCollectList", async (req, res) => {
    let { user_id, comment_db } = req.body;
    console.log(user_id, comment_db);
    await newsDetail
        .selectCollectList(user_id, comment_db)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
// addNews
router.post("/addNews", async (req, res) => {
    let { title, source, digest, create_time, content, class_id, hot } = req.body;
    console.log(title, source, digest, create_time, content, class_id, hot);
    await newsDetail
        .addNews(title, source, digest, create_time, content, class_id, hot)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});

router.post("/newsDetailPaging", async (req, res) => {
    let { page_num, page_size } = req.body;

    await newsDetail
        .newsDetailPaging(page_num, page_size)
        .then((content) => {
            res.send(content);
        })
        .catch((err) => {
            res.send(err);
        });
});
router.post("/newsSearchPaging", async (req, res) => {
    // comment_id 新闻id
    // console.log(req.query);
    let { content, page_num, page_size } = req.body;
    console.log(content, page_num, page_size);
    await newsDetail
        .newsSearchPaging2(content, page_num, page_size)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
router.get("/newsDetailTotal", async (req, res) => {
    await newsDetail
        .newsDetailTotal()
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
router.post("/delNews", async (req, res) => {
    let { id } = req.body;

    await newsDetail
        .delNews(id)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
// parent_id, comment_id, content, create_time, user_id, x
router.post("/addSonComment", async (req, res) => {
    let token = req.headers["token"];
    let jwt = new JwtUtil(token);
    let result = jwt.verifyToken();
    console.log(result, "token");
    if (result == "err") {
        res.send({
            code: -1,
            msg: "登录已过期,请重新登录。",
        });
    } else {
        let { parent_id, comment_id, content, create_time, user_id, db_id } = req.body;
        console.log("req.body", parent_id, comment_id, content, create_time, user_id, db_id);
        await newsDetail
            .addSonComment(parent_id, comment_id, content, create_time, user_id, db_id)
            .then((data) => {
                res.send(data);
            })
            .catch((error) => {
                res.send(error);
            });
    }
});
router.post("/selectSonComment", async (req, res) => {
    let { parent_id } = req.body;
    console.log(parent_id);
    await newsDetail
        .selectSonComment(parent_id)
        .then((data) => {
            res.send(data);
        })
        .catch((error) => {
            res.send(error);
        });
});
module.exports = router;
